//
//  FYFImageGroupeView.m
//  FYFPhotoBrowser
//
//  Created by fuyoufang on 16/5/14.
//  Copyright © 2016年 深圳－真二网. All rights reserved.
//

#import "FYFImageGroupeView.h"
#import <Masonry.h>
#import "FYFImageInfo.h"
#import <UIImageView+WebCache.h>
#import "FYFPhotoBrowser.h"

/** 图片之间的间距 */
#define ImageViewMargin 10
/** 每一行最大的图片数量 */
#define ImageViewMaxRowCount 3

@interface FYFImageGroupeView ()

@property (nonatomic, strong) NSMutableArray *imageViews;

@end

@implementation FYFImageGroupeView

#pragma mark - getter

- (NSMutableArray *)imageViews {
    if (_imageViews == nil) {
        _imageViews = [NSMutableArray array];
    }
    return _imageViews;
}

#pragma mark - setter

- (void)setImageInfos:(NSArray *)imageInfos {

    NSInteger currentCount = _imageInfos.count;
    NSInteger targetCount = imageInfos.count;
    NSInteger originalCount = currentCount;
    
    _imageInfos = imageInfos;
    
    while (targetCount != currentCount) {
        if (targetCount > currentCount) {
            UIImageView *imageView = [[UIImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.backgroundColor = [UIColor orangeColor];
            imageView.tag = currentCount;
            imageView.userInteractionEnabled = YES;
            UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewTaped:)];
            [imageView addGestureRecognizer:tapGestureRecognizer];
            [self addSubview:imageView];
            
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self.mas_width).multipliedBy(1 / 3.0).mas_offset(-((2 / 3.0) * ImageViewMargin));
                make.height.equalTo(self.mas_width).multipliedBy(1 / 3.0).mas_offset(-((2 / 3.0) * ImageViewMargin));
                if (currentCount % 3 == 0) {
                    make.left.equalTo(self.mas_left);
                } else {
                    make.left.equalTo(self.mas_right).multipliedBy((currentCount % 3) / 3.0).mas_offset(ImageViewMargin * (currentCount % 3) / 3.0);
                }
                if (currentCount / 3 == 0) {
                    make.top.equalTo(self.mas_top);
                } else {
                   make.top.equalTo(self.mas_right).multipliedBy((currentCount / 3) / 3.0).mas_offset(ImageViewMargin * (currentCount / 3) / 3.0);
                    
                }
            }];
            [self.imageViews addObject:imageView];
            currentCount++;
        } else {
            UIImageView *imageView = [self.imageViews lastObject];
            [imageView removeFromSuperview];
            [self.imageViews removeLastObject];
        }
    }
    
    if (originalCount != targetCount) {
        if (originalCount == 4) {
            UIImageView *imageView = self.imageViews[2];
            [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top);
                make.left.equalTo(imageView.mas_width).multipliedBy((2 % 3) / 3).mas_offset(ImageViewMargin * (2 % 3) / 3);
            }];
            
            imageView = self.imageViews[3];
            [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(imageView.mas_height).mas_offset(ImageViewMargin);
                make.left.equalTo(self.mas_left);
            }];
        } else if (targetCount == 4) {
            UIImageView *imageView = self.imageViews[2];
            [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(imageView.mas_height).mas_offset(ImageViewMargin);
                make.left.equalTo(self.mas_left);
            }];
            
            imageView = self.imageViews[3];
            [imageView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(imageView.mas_height).mas_offset(ImageViewMargin);
                make.left.equalTo(imageView.mas_width).multipliedBy((5 % 3) / 3).mas_offset(ImageViewMargin * (5 % 3) / 3);
            }];
        }
    }
    
    for (NSInteger index = 0; index < self.imageViews.count; index++) {
        UIImageView *imageView = self.imageViews[index];
        FYFImageInfo *imageInfo = imageInfos[index];
        if (imageInfo.thumbnailURLString) {
            [imageView sd_setImageWithURL:[NSURL URLWithString:imageInfo.thumbnailURLString] placeholderImage:[UIImage imageNamed:@""]];
        }
    }
}

#pragma mark - systerm


#pragma mark - other
- (void)imageViewTaped:(UITapGestureRecognizer *)tapGestureRecognizer {
    UIImageView *imageView = (UIImageView *)tapGestureRecognizer.view;
    
    if ([self.delegate respondsToSelector:@selector(imageGroupeView:didTapImageViewAtIndex:imageView:)]) {
        [self.delegate imageGroupeView:self didTapImageViewAtIndex:imageView.tag imageView:imageView];
    }
}





@end
